package com.fenrirtec.aepp.manage.action;

import java.io.File;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.fenrirtec.aepp.common.dto.AttachmentDto;
import com.fenrirtec.aepp.common.dto.SystemConfigDto;
import com.fenrirtec.aepp.common.service.FileService;
import com.fenrirtec.aepp.common.service.TenderDocService;
import com.fenrirtec.aepp.common.service.impl.FileServiceImpl;
import com.fenrirtec.aepp.common.service.impl.TenderDocServiceImpl;
import com.fenrirtec.aepp.common.support.JSONActionSupport;
import com.fenrirtec.core.definition.BaseDefinition.API;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;

public class TenderDocUploadAction extends JSONActionSupport {

	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(TenderDocUploadAction.class);

	private File tenderDoc;
	
	private String tenderDocFileName;
	
	private String param;

	public File getTenderDoc() {
		return tenderDoc;
	}

	public void setTenderDoc(File tenderDoc) {
		this.tenderDoc = tenderDoc;
	}

	public String getTenderDocFileName() {
		return tenderDocFileName;
	}

	public void setTenderDocFileName(String tenderDocFileName) {
		this.tenderDocFileName = tenderDocFileName;
	}

	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public TenderDocUploadAction() {
	}

	@Override
	protected List<ErrorInfo> doValidate() {
		return null;
	}

	@Override
	protected JSONObject doExecute() {
	    
	    FileService fileService = new FileServiceImpl();
	    TenderDocService tenderDocService = new TenderDocServiceImpl();
	    Boolean result = false;
		
		try {
			
			JSONObject jsonObj = JSONObject.parseObject(this.param);
			Integer tenderId = jsonObj.getInteger("tender_id");
			AttachmentDto attachmentDto = new AttachmentDto();
			String ext = tenderDocFileName.substring(tenderDocFileName.lastIndexOf(".") + 1);
			if ("xls".equals(ext) || "xlsx".equals(ext)) {
			    attachmentDto.setFileType(2);
			} else if ("doc".equals(ext) || "docx".equals(ext)) {
                attachmentDto.setFileType(3);
            } else if ("pdf".equals(ext)) {
                attachmentDto.setFileType(5);
            }
			attachmentDto.setFileCategory(5);
			
			SystemConfigDto config = getSystemConfig();
			String uploadRootPath = config.getUploadRootFolder();
			File saveFolder = new File(uploadRootPath, "/tender_doc_tmp");
			if (!saveFolder.exists()) {
                saveFolder.mkdirs();
            }
			String fileName = "tender_doc_" + tenderId.toString() + "." + ext;
            String path = uploadRootPath + "/tender_doc_tmp/" + fileName;
            attachmentDto.setPath(path);
            attachmentDto.setComment("标书模板文件");
            attachmentDto.setUploadUser(getUserInfo().getLoginName());
            Integer attachmentId = tenderDocService.upload(tenderId, attachmentDto);
            if (attachmentId != null && attachmentId.intValue() > 0) {
                fileService.copyFile(tenderDoc.getPath(), path);
                tenderDoc.deleteOnExit();
                result = true;
            }
            
			JSONObject root = new JSONObject();
			root.put("result", result);
			return root;

		} catch (DatabaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error("database error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} catch (UnexpectedException e) {
			if (logger.isErrorEnabled()) {
				logger.error("unexpected error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		}
	}
}
